package com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.publicOperate.service.impl;


import com.alibaba.fastjson.JSON;
import com.css.fxfzfxqh.common.rest.JcsjRestManager;
import com.css.fxfzfxqh.constants.FxfzConstants;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.DistrictEntity;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.publicOperate.service.DistrictService;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author zyt
 * @Date: 2023/10/23
 */
@Service
@Slf4j
public class DistrictServiceImpl implements DistrictService {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Autowired
    JcsjRestManager jcsjRestManager;

    //根据经纬度，查询行政区划--省|市|区县的名称和code列表
    @Override
    public List<DistrictEntity> queryProvinceCityCountyNameCodeListByLonLat(String lon, String lat){
        List<DistrictEntity> resultList = new ArrayList<>();
        try {
            if (!PlatformObjectUtils.isEmpty(lon) && !PlatformObjectUtils.isEmpty(lat)) {
                String geomText = "POINT("+lon+" "+lat+")";
                //查询行政区划
                String sql = "select c.name as province,c.code as code1,b.name as city,b.code as code2,a.name as county,a.code as code3 from district_boundary_county a " +
                        "left join district_boundary_city b on b.code=concat(subString(a.code,0,5),'00') " +
                        "left join district_boundary_province c on c.code=concat(subString(a.code,0,3),'0000') " +
                        "where ST_Intersects(a.geom,ST_GeometryFromText('"+geomText+"',4490)) " +
                        "order by a.code,b.code,c.code;";
                resultList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DistrictEntity.class));
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }

        return resultList;
    }

    //根据地图圈选区域，查询行政区划--省|市|区县的名称和code列表
    @Override
    public List<DistrictEntity> queryProvinceCityCountyNameCodeListByGeomText(String geomText){
        List<DistrictEntity> resultList = new ArrayList<>();
        try {
            if (!PlatformObjectUtils.isEmpty(geomText)) {
                //查询行政区划
                String sql = "select c.name as province,c.code as code1,b.name as city,b.code as code2,a.name as county,a.code as code3 from district_boundary_county a " +
                        "left join district_boundary_city b on b.code=concat(subString(a.code,0,5),'00') " +
                        "left join district_boundary_province c on c.code=concat(subString(a.code,0,3),'0000')," +
                        "(select ST_GeometryFromText('"+geomText+"',4490) as geom) t " +
                        "where ST_Intersects(a.geom,t.geom) " +
                        "and ST_Touches(a.geom,t.geom)!='t' " +
                        "order by a.code,b.code,c.code;";
                resultList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DistrictEntity.class));
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }

        return resultList;
    }

    @Override
    public DistrictEntity getByCode(String code) {
        String sql = "select *,st_astext(geom) geomText from district_boundary_county where code='"+code+"'";
        List<DistrictEntity> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DistrictEntity.class));
        return PlatformObjectUtils.isNotEmpty(query)?query.get(0):null;
    }

    @Override
    public String getUnionGeomByCodeList(List<String> codeList) throws BizException {
        return jcsjRestManager.getUnionGeomByCodeList(codeList);
    }
}
